/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package VMTermClassSettingsDialog;


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

import GuiTools.*;
import Mainframe.Mainframe;
import Settings.*;
import Storage.*;
import VocabularyManagement.EditVocDialog;

/**
 * A vocabulary's term class settings can be edited here.
 * 
 * @author Christoph Schnack
 */

public class VocabularyTermClassManagement{
	private TermClassPanel tcPanelA,tcPanelB;
	private MDTribuitFrame vsdFrame;
	private String vocTE;
	private Vocabulary vocToEdit;
	private Settings SET;
	private Storage MAINVOC;
	private Mainframe MAINFRAME;
	private Language LANG;
	private MDManager VMDM;
	private boolean changesMade = false;
	
	public VocabularyTermClassManagement(String vte,Storage mainvoc,Settings set,Mainframe mf,MDManager vmdm){
		MAINVOC=mainvoc;
		SET=set;
		LANG=SET.getLanguage();
		MAINFRAME=mf;
		VMDM=vmdm;
		vocTE=vte;
		vsdFrame = new MDTribuitFrame(LANG.getTermclasses()+" "+LANG.getSettings(),VMDM);
		vocToEdit = MAINVOC.getVocabulary(vocTE);
	//center panels
		tcPanelA = new TermClassPanel(mainvoc, set, this, vocToEdit.getNameA(), vocToEdit, vocToEdit.getTermclassesA(), vocToEdit.getSortedTermClassKeysA());
		tcPanelB = new TermClassPanel(mainvoc, set, this, vocToEdit.getNameB(), vocToEdit, vocToEdit.getTermclassesB(), vocToEdit.getSortedTermClassKeysB());
				
	//Buttonpanel
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(GuiTools.BGCOLOR);
		JButton cancelButton = new JButton(LANG.getDiscardAndCancel());
		cancelButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				vsdFrame.dispose();
				new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
			}
		});
		JButton applyButton = new JButton(LANG.getApply());
		applyButton.addActionListener(new ApplyBtnListener());
		buttonPanel.add(applyButton);
		buttonPanel.add(cancelButton);
		
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill=  GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.insets = new Insets(20,20,20,0);
		centerPanel.add(tcPanelA,gbc);
		gbc.gridx=1;
		gbc.insets = new Insets(20,20,20,20);
		centerPanel.add(tcPanelB,gbc);
		centerPanel.setBackground(Color.darkGray);
		
		CustomScrollPane jsp = new CustomScrollPane(centerPanel);
		if(GuiTools.getScreenSize().height<768){
			jsp.setPreferredSize(new Dimension(800,550));
		}
		
		vsdFrame.getContentPane().add(BorderLayout.NORTH, new Header(LANG.getTermclasses()+" "+LANG.getSettings()));
		vsdFrame.getContentPane().add(BorderLayout.CENTER, jsp);
		vsdFrame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
		vsdFrame.addWindowListener(new WindowAdapter() {
		      public void windowClosing(WindowEvent e){
		    	  new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
		      }
		});
		vsdFrame.setResizable(true);
		vsdFrame.showUpAt(MAINFRAME);
	}
	
	public TribuitFrame getFrame(){
		return vsdFrame;
	}
	
	public void setChangesMade(boolean arg){
		changesMade = arg;
	}
	
	private class ApplyBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			vsdFrame.setVisible(false);
			if(changesMade){
				if(vocToEdit.hasWords()){
					Vector<String> editedTermClassesNamesA = new Vector<String>(), usedTermClassesA = vocToEdit.getUsedTermClassesA(),editedTermClassesNamesB = new Vector<String>(), usedTermClassesB = vocToEdit.getUsedTermClassesB();;
					usedTermClassesA.trimToSize();
					usedTermClassesB.trimToSize();
					for(String key : vocToEdit.getTermclassesA().keySet().toArray(new String[]{})){
						if(usedTermClassesA.contains(key)){
							if(tcPanelA.getSortedTermClassKeys().contains(key)){
								if(vocToEdit.getTermclassA(key).hasPrefix() && tcPanelA.getTermClasses().get(key).hasPrefix()){
									if(!compareEquityOfNameValue(tcPanelA.getTermClasses().get(key).getPreFixName(),tcPanelA.getTermClasses().get(key).getPreFixVal(),vocToEdit.getTermclassA(key).getPreFixName(),vocToEdit.getTermclassA(key).getPreFixVal())){
										editedTermClassesNamesA.add(key);
										continue;
									}
								}
								else{
									if(vocToEdit.getTermclassA(key).hasPrefix() != tcPanelA.getTermClasses().get(key).hasPrefix()){
										editedTermClassesNamesA.add(key);
										continue;
									}
								}
								if(vocToEdit.getTermclassA(key).getKeys()!=null &&  tcPanelA.getTermClasses().get(key).getKeys()!=null){
									if(tcPanelA.getTermClasses().get(key).getSortedKeys().size() != vocToEdit.getTermclassA(key).getSortedKeys().size()){
										editedTermClassesNamesA.add(key);
										continue;
									}
									for(String name : vocToEdit.getTermclassA(key).getKeys()){
										if(!tcPanelA.getTermClasses().get(key).getSortedKeys().contains(name)){
											editedTermClassesNamesA.add(key);
											continue;
										}
										if(!compareEquityOfNameValue(name, vocToEdit.getTermclassA(key).getValues(name),name,tcPanelA.getTermClasses().get(key).getValues(name))){
											editedTermClassesNamesA.add(key);
											continue;
										}
									}
								}
								else{
									if((vocToEdit.getTermclassA(key).getKeys()==null) !=  (tcPanelA.getTermClasses().get(key).getKeys()==null)){
										editedTermClassesNamesA.add(key);
										continue;
									}
								}
							}
							else{
								editedTermClassesNamesA.add(key);
								continue;
							}
						}
					}
					for(String key : vocToEdit.getTermclassesB().keySet().toArray(new String[]{})){
						if(usedTermClassesB.contains(key)){
							if(tcPanelB.getSortedTermClassKeys().contains(key)){
								if(vocToEdit.getTermclassB(key).hasPrefix() && tcPanelB.getTermClasses().get(key).hasPrefix()){
									if(!compareEquityOfNameValue(tcPanelB.getTermClasses().get(key).getPreFixName(),tcPanelB.getTermClasses().get(key).getPreFixVal(),vocToEdit.getTermclassB(key).getPreFixName(),vocToEdit.getTermclassB(key).getPreFixVal())){
										editedTermClassesNamesB.add(key);
										continue;
									}
								}
								else{
									if(vocToEdit.getTermclassB(key).hasPrefix() != tcPanelB.getTermClasses().get(key).hasPrefix()){
										editedTermClassesNamesB.add(key);
										continue;
									}
								}
								if(vocToEdit.getTermclassB(key).getKeys()!=null &&  tcPanelB.getTermClasses().get(key).getKeys()!=null){
									if(tcPanelB.getTermClasses().get(key).getSortedKeys().size() != vocToEdit.getTermclassB(key).getSortedKeys().size()){
										editedTermClassesNamesB.add(key);
										continue;
									}
									for(String name : vocToEdit.getTermclassB(key).getKeys()){
										if(!tcPanelB.getTermClasses().get(key).getSortedKeys().contains(name)){
											editedTermClassesNamesB.add(key);
											continue;
										}
										if(!compareEquityOfNameValue(name, vocToEdit.getTermclassB(key).getValues(name),name,tcPanelB.getTermClasses().get(key).getValues(name))){
											editedTermClassesNamesB.add(key);
											continue;
										}
									}
								}
								else{
									if((vocToEdit.getTermclassB(key).getKeys()==null) !=  (tcPanelB.getTermClasses().get(key).getKeys()==null)){
										editedTermClassesNamesB.add(key);
										continue;
									}
								}
							}
							else{
								editedTermClassesNamesB.add(key);
								continue;
							}
						}
					}
					if(editedTermClassesNamesA.size()>0 || editedTermClassesNamesB.size()>0){
						new ApplyTCChangesDialog(vocToEdit,MAINVOC,SET,MAINFRAME,VMDM,vsdFrame,editedTermClassesNamesA,tcPanelA.getTermClasses(),tcPanelA.getSortedTermClassKeys(),editedTermClassesNamesB,tcPanelB.getTermClasses(),tcPanelB.getSortedTermClassKeys());
						return;
					}
				}
				vocToEdit.setTermclasses(tcPanelA.getTermClasses(), tcPanelB.getTermClasses());
				vocToEdit.setSortedTermClassKeysA(tcPanelA.getSortedTermClassKeys());
				vocToEdit.setSortedTermClassKeysB(tcPanelB.getSortedTermClassKeys());
				MAINVOC.saveVocabularyIfAutosave(vocToEdit);
				vsdFrame.dispose();
				new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
			}
		}
	}

	private boolean compareEquityOfNameValue(String nameA, Vector<String> valueA, String nameB, Vector<String> valueB){
		if(!nameA.equals(nameB) ){
			return false;
		}
		if(valueA==null && valueB==null) return true;
		if(valueA==null && valueB!=null){
			return false;
		}
		else{
			if(valueB == null){
				return false;
			}
			if(valueA.size() != valueB.size()){
				return false;
			}
			for(String value : valueA){
				if(!valueB.contains(value)){
					return false;
				}
			}
		}
		return true;
	}
}
